﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace Includes
{
   /// <summary>
   /// HCS project specific wrapper for the Tropo cloud API
   /// </summary>
   public static class TropoWrapper
   {
      // If TESTMODE is true, only the 2 designated Testnumbers will actually be called.
      // All other numbers will directly fake an Informed SMS received file.
      const bool TESTMODE = true;


      /// <summary>
      /// Places a voice call reading the containing message to the containing receipient 
      /// </summary>
      /// <param name="call">Call struct that is used to make the call </param>
      /// <returns> true if call has been received (declined or accepted), false otherwise </returns>
      public static bool sendVoiceCall(VoiceCall call)
      {
         // During the test, only the 2 registered numbers ACTUALLY get called.
         if((call.receipientNumber != "+4917662719507" || 
            call.receipientNumber != "+491735479287")
            && TESTMODE)
         {
            try
            {
               // Create event file
               System.IO.Directory.CreateDirectory("Info");
               System.IO.File.Create("Info\\" + call.receipientNumber);

               Console.WriteLine("FAKE INFO (" + call.receipientNumber + ") WRITTEN");
            }
            catch (Exception)
            {
               Console.WriteLine("Event SMS could not be translated");
            }
            return true;
         }


         try
         {
            String requestURL = "http://api.tropo.com/1.0/sessions?action=create&token=112fa4bd4ac2f44d9f08ea2ca60abd1331106b465aaecc675094295fa13e3f71e851d097689149c6012ec0d2&receipientNumber=" + call.receipientNumber.Substring(1) + "&alternative=" + call.alternative + "&fileID=" + call.fileID +"&senderNumber=" + call.senderNumber;

            HttpWebRequest  theRequest  = (HttpWebRequest)  WebRequest.Create(requestURL);
            HttpWebResponse theResponse = (HttpWebResponse) theRequest.GetResponse();
         
            return true;
         }
         catch(Exception)  
         {
            return false;
         }
      }
   }
}
